package com.huikeportal.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huikeportal.dao.LoginMapper;
import com.huikeportal.dao.SiteDataManagerMapper;
import com.huikeportal.model.MessageCommon;
import com.huikeportal.model.PosDataManagerInfo;
import com.huikeportal.model.RegionType;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.SiteDataManagerInfo;
import com.huikeportal.model.SiteNode;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.common.PageDataVo;
import com.huikeportal.service.SiteService;

@Service("siteService")
public class SiteServiceImpl implements SiteService {

	@Autowired
	private SiteDataManagerMapper siteDataManagerMapper;
	@Autowired
	private LoginMapper loginMapper;
	
	/**
	 * 店铺数据管理
	 */
	public PageDataVo getSiteDataList(PageDataVo pageDataVo) throws Exception {
		int maxTotal = siteDataManagerMapper.querySiteDataTotal(pageDataVo);
		List<SiteDataManagerInfo> siteDataList = siteDataManagerMapper
				.querySiteDataList(pageDataVo);
		pageDataVo.setMaxRow(maxTotal);
		pageDataVo.setMaxPage((maxTotal + pageDataVo.getPageSize() - 1)
				/ pageDataVo.getPageSize());
		pageDataVo.setPageResult(siteDataList);
		return pageDataVo;
	}

	public List<SiteAllInfo> getSitesByUser(UserInfo userInfo) throws Exception {
		return siteDataManagerMapper.getSitesByUser(userInfo);
	}
	
	public MessageCommon updateSiteData(SiteDataManagerInfo siteDataManagerInfo)
				throws Exception {
			siteDataManagerMapper.updateSiteData(siteDataManagerInfo);
			
			MessageCommon message = new MessageCommon();
			message.setFlag(true);
			message.setMessage("更新店铺基础数据成功！");
			// 插入日志表
			return message;
	}
	

	public SiteDataManagerInfo queryBySiteId(String siteId)
				throws Exception {
			return siteDataManagerMapper.queryBySiteId(siteId);
	}

	private String getUserType(UserInfo userInfo) throws Exception {
		if (userInfo.getCreateUserId() != null) {
			UserInfo uuInfo = new UserInfo();
			uuInfo.setUserName(userInfo.getCreateUserId());
			uuInfo = loginMapper.getUserByName(uuInfo);
			return getUserType(uuInfo);
		}
		return userInfo.getUserType();
	}
	
	public PosDataManagerInfo getSiteDataById(
			PosDataManagerInfo siteDataManangerInfo) throws Exception {
		return null;
	}
	
	public List<RegionType> getFloors(String squareId) throws Exception {
		return siteDataManagerMapper.getFloorBySquareId(squareId);
	}

	public List<RegionType> getProvinces(String countryId) throws Exception {
		return siteDataManagerMapper.getProvincesByCountryId(countryId);
	}

	public List<RegionType> getCitys(String provinceId) throws Exception {
		return siteDataManagerMapper.getCitysByProvinceId(provinceId);
	}
	
	public List<RegionType> getAreas(String cityId) throws Exception {
		return siteDataManagerMapper.getAreasByCityId(cityId);
	}
	
	public List<SiteNode> getSiteByUser(UserInfo userInfo) throws Exception {
		return siteDataManagerMapper.getSiteByUser(userInfo);
	}
	
	public int getLowPermission(List<SiteAllInfo> siteList){
		int permission = 1000;
		for(int i=0,len = siteList.size();i<len;i++){
			if(siteList.get(i).getType()!= null){
				int currentPermission = siteList.get(i).getType();
				permission = permission < currentPermission ?  permission : currentPermission;
			}
		}
		
		return permission;
	}
	
	// 获取权限下的所有广场
	public List<SiteAllInfo> getAllGcList(List<SiteAllInfo> siteList) throws Exception {
		List<SiteAllInfo> allGcList = new ArrayList<SiteAllInfo>();
		for(int i=0,len = siteList.size(); i<len; i++){
			SiteAllInfo temp = siteList.get(i);
			int type = temp.getType();
			if(type == 0){
				List<SiteAllInfo> jtList = siteDataManagerMapper.getAllJtSites(temp);
				for(int j=0,length=jtList.size();j<length;j++){
					SiteAllInfo current = jtList.get(j);
					if(current.getType() == 200){
						List<SiteAllInfo> dyList = siteDataManagerMapper.getAllDySitesByJt(current);
						for(int m=0;m<dyList.size();m++){
							List<SiteAllInfo> gcList = siteDataManagerMapper.getAllGcSitesByDy(dyList.get(m));
							allGcList.addAll(gcList);
						}
					}
					List<SiteAllInfo> gcList = siteDataManagerMapper.getAllGcSitesByJt(current);
					allGcList.addAll(gcList);
				}
			}else if(type == 100){
				List<SiteAllInfo> gcList = siteDataManagerMapper.getAllGcSitesByJt(temp);
				allGcList.addAll(gcList);
			}else if(type == 200){
				List<SiteAllInfo> gcList = siteDataManagerMapper.getAllGcSitesByDy(temp);
				allGcList.addAll(gcList);
			}else if(type == 300){
				// 当用户拥有店铺权限和广场权限时，如果该店铺不属于广场则添加店铺，否则不添加
				int length = allGcList.size();
				boolean flag = false;
				for(int j=0;j<length;j++){
					String siteKey = allGcList.get(j).getSiteid();
					if(siteKey.equals(temp.getSiteid())){
						flag = true;
					}
				}
				if(!flag){
					allGcList.add(temp);
				}
			}
		}
		return allGcList;
	}
	
	// 获取广场下的所有场所
	public List<SiteAllInfo> getAllSiteList(List<SiteAllInfo> siteList) throws Exception{
		List<SiteAllInfo> allSiteList = new ArrayList<SiteAllInfo>();
		for(int i=0,len=siteList.size();i<len;i++){
			SiteAllInfo temp = siteList.get(i);
			if(temp.getType()==300){
				List<SiteAllInfo> tempList = siteDataManagerMapper.getAllSitesByGc(temp);
				allSiteList.addAll(tempList);
			}else if(temp.getType()>300){
				allSiteList.add(temp);
			}
		}
		return allSiteList;
	}
	
	// 获取广场下的所有店铺
	public List<SiteAllInfo> getStoresByGc(List<SiteAllInfo> gcList) throws Exception{
		List<SiteAllInfo> storeList = new ArrayList<SiteAllInfo>();
		for(int i=0,len=gcList.size();i<len;i++){
			SiteAllInfo temp = gcList.get(i);
			List<SiteAllInfo> tempList = siteDataManagerMapper.getStoresByGc(temp);
			storeList.addAll(tempList);
		}
		return storeList;
	}
	
	// 获取广场下的楼层
	public List<SiteAllInfo> getFloorsByGc(List<SiteAllInfo> gcList) throws Exception{
		List<SiteAllInfo> floorList = new ArrayList<SiteAllInfo>();
		for(int i=0,len=gcList.size();i<len;i++){
			SiteAllInfo temp = gcList.get(i);
			List<SiteAllInfo> tempList = siteDataManagerMapper.getFloorsByGc(temp);
			floorList.addAll(tempList);
		}
		return floorList;
	}
	
	// 获取楼层下的所有店铺
	public List<SiteAllInfo> getStoresByFloor(List<SiteAllInfo> floorList) throws Exception{
		List<SiteAllInfo> storeList = new ArrayList<SiteAllInfo>();
		for(int i=0,len=floorList.size();i<len;i++){
			SiteAllInfo temp = floorList.get(i);
			List<SiteAllInfo> tempList = siteDataManagerMapper.getStoresByFloor(temp);
			storeList.addAll(tempList);
		}
		return storeList;
	}
	
	public String getSitePermitAreaStr(List<SiteAllInfo> sitePermitList){
		StringBuffer buffer = new StringBuffer();
		for(int i=0,len=sitePermitList.size();i<len;i++){
			SiteAllInfo temp = sitePermitList.get(i);
			buffer.append(temp.getSiteid());
			buffer.append(",");
		}
		return buffer.toString();
	}
	
	
	/**
	 * 导航栏日期
	 * 当前时间：2015-11-16 下午
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	public String getDate() throws Exception{
		Date date=new Date();
		DateFormat format1=new SimpleDateFormat("yyyy-MM-dd");
		String str=format1.format(date);
		int hh=date.getHours();
		String restr = "";
		if(hh<12){
			restr="当前时间："+str+" 上午 ";
		}
		else
		{
			restr="当前时间："+str+" 下午 ";
		}
		return restr;
	}

	public List<RegionType> filterSquare(String provinceId, String cityId,
			String areaId, List<SiteAllInfo> squareList) throws Exception {
		List<RegionType> resultList = new ArrayList<RegionType>();
		for(int i=0,len=squareList.size();i<len;i++){
			SiteAllInfo info = squareList.get(i);
			if(provinceId == null || "-1".equals(provinceId)){
				RegionType region = new RegionType();
				region.setRegionId(info.getSiteid());
				region.setRegionName(info.getSitename());
				resultList.add(region);
				continue;
			}else if(areaId != null && areaId.equals(String.valueOf(info.getAreaid()))){
				RegionType region = new RegionType();
				region.setRegionId(info.getSiteid());
				region.setRegionName(info.getSitename());
				resultList.add(region);
				continue;
			}else if(cityId != null && cityId.equals(info.getCityid())){
				RegionType region = new RegionType();
				region.setRegionId(info.getSiteid());
				region.setRegionName(info.getSitename());
				resultList.add(region);
				continue;
			}else if(provinceId != null && provinceId.equals(String.valueOf(info.getProvinceid()))){
				RegionType region = new RegionType();
				region.setRegionId(info.getSiteid());
				region.setRegionName(info.getSitename());
				resultList.add(region);
				continue;
			}
		}
		
		return resultList;
	}
	
	
}
